package com.qt.manager.message;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qt.mapper.SystemNoticeMapper;
import com.qt.pojo.entity.message.SystemNotice;
import com.qt.pojo.entity.message.UserNotice;
import com.qt.service.message.SystemNoticeService;
import com.qt.service.message.UserNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author pgw
 * @version 1.0
 * @description
 * @date Create by 2023/9/23 15:09
 */
@Component
public class SystemNoticeManager {

    @Autowired
    private SystemNoticeMapper systemNoticeMapper;

    @Autowired
    private UserNoticeService userNoticeService;


    @Async
    public void systemNoticeToNewRegitser(String userId) {
        LambdaQueryWrapper<SystemNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(SystemNotice::getType, "All")
                .le(SystemNotice::getGmtCreate, new Date())
                .eq(SystemNotice::getState, 1);
        List<SystemNotice> systemNotices = systemNoticeMapper.selectList(queryWrapper);
        List<UserNotice> userNoticeList = new ArrayList<>();
        for (SystemNotice systemNotice : systemNotices) {
            UserNotice userNotice = new UserNotice();
            userNotice.setType("System")
                    .setSystemNoticeId(systemNotice.getId())
                    .setReceiveId(userId);
            userNoticeList.add(userNotice);
        }
        userNoticeService.saveOrUpdateBatch(userNoticeList);

    }
}
